/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import businessObjects.Pais;
import daos.PaisDAO;
import daos.exceptions.IllegalOrphanException;
import daos.exceptions.NonexistentEntityException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author williamprado
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PaisServices implements PaisServicesLocal {
    
    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @PostConstruct
    public void initialize(){
    entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void cleanUp(){
        if(entityManager.isOpen()){
            entityManager.close();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void nuevoPais(Pais pais) {
        PaisDAO pd = new PaisDAO();
        try {
            pd.create(pais, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Pais consultarPais(String id) {
        PaisDAO pd = new PaisDAO();
        Pais pais = new Pais();
        try {
            pais = pd.findPais(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pais;
    }

    public Pais buscarPaisPorNombre(String nombre){
        PaisDAO pd = new PaisDAO();
        Pais pais = new Pais();
        pais = pd.findPaisByName(nombre, entityManager);
        return pais;
    }

    public void actualizarPais(Pais pais) {
        PaisDAO pd = new PaisDAO();
        try {
            pd.edit(pais, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void eliminarPais(String id) {
        PaisDAO pd = new PaisDAO();
        try {
            pd.destroy(id, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Pais> todosLosPaises(){
        PaisDAO pd = new PaisDAO();
        List<Pais> paisesList;
        try {
            paisesList = pd.findPaisEntities(entityManager);
            return paisesList;
        } catch (Exception ex) {
            Logger.getLogger(PaisServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

}
